#!/usr/bin/env bash
unset LD_PRELOAD
set -e

# Set the default command
command=("proot"
         "--link2symlink"
         "-0"
         "-r ubuntu-fs"
         "-b /dev"
         "-b /proc"
         "-b ubuntu-fs/root:/dev/shm"
         "-w /root"
         "/usr/bin/env"
         "-i"
         "HOME=/root"
         "PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games"
         "TERM=$TERM"
         "LANG=C.UTF-8"
         "/bin/bash"
         "--login")

# Check if the binds directory exists, and if so, add the binds to the command
if [ -d "binds" ]; then
    for f in binds/* ;do
        . "$f"
    done
fi

# Check if the user wants to mount the home directory of Termux
if [ -n "$TERMUX_HOME_MOUNT" ]; then
    command+=("-b" "/data/data/com.termux/files/home:/root")
fi

# Check if the user wants to mount the sdcard directory directly to /
if [ -n "$TERMUX_SDCARD_MOUNT" ]; then
    command+=("-b" "/sdcard")
fi

# Check if the user has provided any arguments, and if so, add them to the command
if [ $# -gt 0 ]; then
    command+=("-c" "$*")
fi

# Run the command
if [ -n "$TERMUX_FISH" ]; then
    exec fish "${command[@]}"
else
    exec "${command[@]}"
fi